package com.medic.client.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.medic.entity.Assitant;
import com.medic.entity.Doctor;
import com.medic.entity.Pharmacist;
import com.medic.entity.User;
import com.medic.entity.Weichat;
import com.medic.service.impl.WeichatServiceImpl;
import com.medic.util.LoggerUtil;
import com.medic.util.StringUtil;

@Controller
@RequestMapping("/client")
@SessionAttributes({ "user", "weichat", "doctor", "pharmacist", "assitant" })
public class CoreController {

	@Resource(name = "weichatService")
	private WeichatServiceImpl weichatService;

	@RequestMapping("/index")
	public String index(String msg) {
		LoggerUtil.debug(msg);

		// 这里获取openId后根据openId判断其是否注册，没有注册则调到注册页面

		return "/client/" + msg;
	}

	@RequestMapping("/core")
	public String core(String openid, Model model) {
		String result = "";

		try {
			LoggerUtil.debug(openid);
			List<Weichat> list = weichatService.getRoleByOpenID(openid);
			if (list.size() <= 0) {
				result = "redirect:/client/index?msg=user-register";
			} else if (list.size() == 1) {
				LoggerUtil.debug(list.size() + "");
				Weichat weichat = list.get(0);
				LoggerUtil.debug(weichat.toString());
				switch (weichat.getRole()) {
				case StringUtil.USER_KEY:
					User user = weichatService.getUserByWeichatid(weichat.getId());
					model.addAttribute(StringUtil.USER_KEY, user);
					LoggerUtil.debug(user.toString());
					result = "redirect:/client/index?msg=user-index";
					break;
				case StringUtil.DOCTOR_KEY:
					Doctor doctor = weichatService.getDoctorByWeichatid(weichat.getId());
					model.addAttribute(StringUtil.DOCTOR_KEY, doctor);
					LoggerUtil.debug(doctor.toString());
					result = "redirect:/client/index?msg=doctor-index";
					break;
				case StringUtil.PHARMACIST_KEY:
					Pharmacist phar = weichatService.getPharmaicstByWeichatid(weichat.getId());
					model.addAttribute(StringUtil.PHARMACIST_KEY, phar);
					LoggerUtil.debug(phar.toString());
					result = "redirect:/client/index?msg=doctor-index";
					break;
				case StringUtil.ASSITANT_KEY:
					Assitant assi = weichatService.getAssitantByWeichatid(weichat.getId());
					model.addAttribute(StringUtil.ASSITANT_KEY, assi);
					LoggerUtil.debug(assi.toString());
					result = "redirect:/client/index?msg=doctor-index";
					break;
				default:
					model.addAttribute("msg", "未找到对应页面");
					result = "/result";
					break;
				}
				model.addAttribute(StringUtil.WECHAT_KEY, weichat);
			} else {
				// 将所有的角色信息都放入，然后跳转页面后让用户自己选择
				model.addAttribute("list", list);
				result = "/client/role-choose";
			}
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.error("error : " + e.getMessage() + " - params : [ openid : " + openid + " ]");
			result = "error";
		}
		return result;
	}

	@RequestMapping("/role")
	public String role(String openid, String role, Model model) {
		String result = "";
		try {
			List<Weichat> list = weichatService.getRoleByOpenID(openid);
			for (Weichat weichat : list) {
				if (weichat.getRole().equals(role)) {
					if (role.equals(StringUtil.USER_KEY)) {
						model.addAttribute(StringUtil.USER_KEY, weichat);
						result = "redirect:/client/index?msg=user-index";
					} else if (role.equals(StringUtil.DOCTOR_KEY)) {
						model.addAttribute(StringUtil.DOCTOR_KEY, weichat);
						result = "redirect:/client/index?msg=doctor-index";
					} else if (role.equals(StringUtil.PHARMACIST_KEY)) {
						model.addAttribute(StringUtil.PHARMACIST_KEY, weichat);
						result = "redirect:/client/index?msg=doctor-index";
					} else {
						model.addAttribute(StringUtil.ASSITANT_KEY, weichat);
						result = "redirect:/client/index?msg=doctor-index";
					}
					break;
				}
			}
		} catch (Exception e) {
			LoggerUtil
					.error("error : " + e.getMessage() + " - params : [ openid : " + openid + ",role : " + role + " ]");
			result = "error";
		}
		return result;
	}

	@RequestMapping("/test")
	public String test() {
		return "/client/test";
	}
}
